<?php

class stFacebookToolboxHook extends sthook_2_1 {

    public function hookCustom() {
        $option = $this->option;
        $method = array($this, 'showstFacebookToolbox');
        add_shortcode('ShowstFacebookToolbox', $method);
        add_shortcode('facebookPost', array(&$this, 'facebook_wall'));
		/*
		 * Edit By Phuc Nguyen
		 */
	        $facebook_like_setting = $option['facebook_like_setting'];
	    	if($facebook_like_setting['user'] == '1')
	        {
	           if($facebook_like_setting['mode'] == 'url')
	    	   {
	    	    	add_filter('the_content', array($this,'page3'));
	    	   }
	    	   else
	    	   {
	    		    add_filter('the_content', array($this,'page'));
	    			add_filter('the_excerpt', array($this,'page2'));
	    	   } 
	        }
                $option_graph = $option['facebook_graph'];
                if($option_graph['user'] == '1')
                {
                    add_action('wp_head', array(&$this, 'meta') );
                }
   				 if($option_graph['user'] == '1')
                {
                   add_action('admin_menu', array(&$this, 'admin_menu'));
                   add_action('edit_post', array(&$this, 'save_postdata'));
                }
        /*
         * End Edit
         */
//        $method = array($this, 'custom_field_create');
//        add_action('admin_menu', $method);
//        $method = array($this, 'custom_field_handle');
//        add_action('edit_post', $method);

//        $method = array($this, 'sample');
//        add_filter('init', $method);
    }
    /*
     * Phuc nguyen edit function
     */


        	function facebook_wall($args) {
                    $option = $this->option;
				$options = $option['facebook_wall'];
		        $args = shortcode_atts(array(
		            'posts'     => $options['post'],
		            'offset'    => $options['offset'],
		            'pageid'  => $options['id']
		            ), $args);
		      $controllerName = $this->config['plugin_name'] . "Controller";
		        $controllerObject = new $controllerName($this->config);
		        return $controllerObject->getAction('display_facebook_wall',$args);

	    	}

	public function page($content)
	{
			global $post;
            $option = $this->option;
            $options = $option['facebook_like_setting'];
            $style = @$options['style'];
            $faces = @$options['faces'];
            $width = @$options['width'];
            $height = @$options['height'];
            $font = @$options['font'];
            $colorscheme = @$options['colorscheme'];
            $verb = @$options['verb'];
			$page = @$options['post'];
			$up = @$options['up'];
			$categories = @$options['categories'];
			$pront =  @$options['pront'];
			$archive = @$options['archive'];
            $server = get_permalink($post->ID);
            $str = "<iframe src='http://www.facebook.com/plugins/like.php?href=".$server."&amp;layout=".$style."&show_faces=".$faces."&width=".$width."&height=".$height."&action=".$verb."&font=".$font."&colorscheme=".$colorscheme."' id='fbLikeIframe' name='fbLikeIframe' scrolling='no' frameborder='0' allowTransparency='true' class='fbLikeContainer' style='border:none; overflow:hidden; width:".$width."px; height:".$height."px; display:inline;' > </iframe>";
            switch ($page)
			{
				case  '1' :
				{
					if(!is_page() && $pront == 'true')
					{
						if($up == '2')
						{
							return $content.$str;
						}
						else
						{
							return $str.$content;
						}
					}
					elseif(is_single())
					{
						if($up == '2')
						{
							return $content.$str;
						}
						else
						{
							return $str.$content;
						}
					}
					else
					{
							return $content;
					}
				}
				case '2' :
				{
					if(is_page())
					{
					    if( (get_bloginfo('url').'/' == $this->curPageURL()) and $pront == 'false')
                        {
                            return $content;
                        }
                        else
                        {
                           if($up == '2')
    						{
    							return $content.$str;
    						}
    						else
    						{
    							return $str.$content;
    						} 
                        }
						
					}
					else
					{
							return $content;
					}
				}
				case '3' : 
				{
					if($pront == 'true' && !is_page())
					{
						if($up == '2')
						{
							return $content.$str;
						}
						else
						{
							return $str.$content;
						}
					}
					elseif(is_single())
					{
						if($up == '2')
						{
							return $content.$str;
						}
						else
						{
							return $str.$content;
						}
					}
					elseif(is_page())
					{
						if( (get_bloginfo('url').'/' == $this->curPageURL()) and $pront == 'false')
                        {
                            return $content;
                        }
                        else
                        {
                           if($up == '2')
    						{
    							return $content.$str;
    						}
    						else
    						{
    							return $str.$content;
    						} 
                        }
					}
					else
					{
							return $content;
					}
				}
			}	
	}
        function page2($content) {
                    $option = $this->option;
                    global $post;
                    $options = $option['facebook_like_setting'];
                    $style = @$options['style'];
                    $faces = @$options['faces'];
                    $width = @$options['width'];
                    $height = @$options['height'];
                    $font = @$options['font'];
                    $colorscheme = @$options['colorscheme'];
                    $verb = @$options['verb'];
                                $page = @$options['post'];
                                $up = @$options['up'];
                                $categories = @$options['categories'];
                                $pront =  @$options['pront'];
                                $archive = @$options['archive'];
                    $server = get_permalink($post->ID);
                    $str = "<iframe src='http://www.facebook.com/plugins/like.php?href=".$server."&amp;layout=".$style."&show_faces=".$faces."&width=".$width."&height=".$height."&action=".$verb."&font=".$font."&colorscheme=".$colorscheme."' id='fbLikeIframe' name='fbLikeIframe' scrolling='no' frameborder='0' allowTransparency='true' class='fbLikeContainer' style='border:none; overflow:hidden; width:".$width."px; height:".$height."px; display:inline;' > </iframe>";

                                                if($categories == 'true' && is_category())
                                                {
                                                                if( (get_bloginfo('url').'/' == $this->curPageURL()) AND $pront == 'false')
                                    {
                                        return $content;
                                    }
                                    else
                                    {
                                       if($up == '2')
                                                                {
                                                                        return $content.$str;
                                                                }
                                                                else
                                                                {
                                                                        return $str.$content;
                                                                }
                                    }
                                                }
                                                elseif($archive == 'true' && !is_category())
                                                {

                                                                if( (get_bloginfo('url').'/' == $this->curPageURL()) AND $pront == 'false')
                                    {
                                        return $content;
                                    }
                                    else
                                    {
                                       if($up == '2')
                                                                {
                                                                        return $content.$str;
                                                                }
                                                                else
                                                                {
                                                                        return $str.$content;
                                                                }
                                    }

                                                }
                                                else
                                                {
                                                        if((get_bloginfo('url').'/' == $this->curPageURL()) and $pront == 'true')
                                                        {
                                                                if($up == '2')
                                                                {
                                                                        return $content.$str;
                                                                }
                                                                else
                                                                {
                                                                        return $str.$content;
                                                                }
                                                        }
                                                        else
                                                        {
                                                                return $content;
                                                        }
                                                }







                }
        function page3($content) {
            global $post;
            $option = $this->option;
            $options = $option['facebook_like_setting'];
            $style = @$options['style'];
            $faces = @$options['faces'];
            $width = @$options['width'];
            $height = @$options['height'];
            $font = @$options['font'];
            $colorscheme = @$options['colorscheme'];
            $verb = @$options['verb'];
			$page = @$options['post'];
			$up = @$options['up'];
			$categories = @$options['categories'];
			$pront =  @$options['pront'];
			$archive = @$options['archive'];
            $server = get_permalink($post->ID);
         $str = "<iframe src='http://www.facebook.com/plugins/like.php?href=".$server."&amp;layout=".$style."&show_faces=".$faces."&width=".$width."&height=".$height."&action=".$verb."&font=".$font."&colorscheme=".$colorscheme."' id='fbLikeIframe' name='fbLikeIframe' scrolling='no' frameborder='0' allowTransparency='true' class='fbLikeContainer' style='border:none; overflow:hidden; width:".$width."px; height:".$height."px; display:inline;' > </iframe>";
			switch ($page)
			{
				case  '1' :
				{
					if(!is_page())
					{
						if(is_category() and $categories == 'true' and !is_home())
						{
							if( (get_bloginfo('url').'/' == $this->curPageURL()) AND $pront == 'false')
                            {
                                return $content;
                            }
                            else
                            {
                               if($up == '2')
        						{
        							return $content.$str;
        						}
        						else
        						{
        							return $str.$content;
        						} 
                            }
						}
						elseif(!is_category() and $archive == 'true' and !is_home() and !is_single())
						{
							if( (get_bloginfo('url').'/' == $this->curPageURL()) AND $pront == 'false')
                            {
                                return $content;
                            }
                            else
                            {
                               if($up == '2')
        						{
        							return $content.$str;
        						}
        						else
        						{
        							return $str.$content;
        						} 
                            }
						}
						elseif((get_bloginfo('url').'/' == $this->curPageURL()) and $pront == 'true')
						{
							if($up == '2')
							{
								return $content.$str;
							}
							else
							{
								return $str.$content;
							}
						}
						else
						{
							return $content;
						}
						
					}
					elseif(is_single())
					{
						if( (get_bloginfo('url').'/' == $this->curPageURL()) AND $pront == 'false')
                        {
                            return $content;
                        }
                        else
                        {
                           if($up == '2')
    						{
    							return $content.$str;
    						}
    						else
    						{
    							return $str.$content;
    						} 
                        }
					}
					else
					{
							return $content;
					}
				}
				case '2' :
				{
					if(is_page())
					{
					   
                       
						if( (get_bloginfo('url').'/' == $this->curPageURL()) AND $pront == 'false')
                        {
                            return $content;
                        }
                        else
                        {
                           if($up == '2')
    						{
    							return $content.$str;
    						}
    						else
    						{
    							return $str.$content;
    						} 
                        }
					}
					else
					{
							return $content;
					}
				}
				case '3' : 
				{
					if(!is_page())
					{
						if(is_category() and $categories == 'true' and !is_home())
						{
							if( (get_bloginfo('url').'/' == $this->curPageURL()) AND $pront == 'false')
                            {
                                return $content;
                            }
                            else
                            {
                               if($up == '2')
        						{
        							return $content.$str;
        						}
        						else
        						{
        							return $str.$content;
        						} 
                            }
						}
						elseif(!is_category() and $archive == 'true' and !is_home() and !is_single())
						{
							if( (get_bloginfo('url').'/' == $this->curPageURL()) AND $pront == 'false')
                            {
                                return $content;
                            }
                            else
                            {
                               if($up == '2')
        						{
        							return $content.$str;
        						}
        						else
        						{
        							return $str.$content;
        						} 
                            }
						}
						elseif((get_bloginfo('url').'/' == $this->curPageURL()) and $pront == 'true')
						{
							if($up == '2')
							{
								return $content.$str;
							}
							else
							{
								return $str.$content;
							}
						}
						else
						{
							return $content;
						}
					}
					elseif(is_single())
					{
						if( (get_bloginfo('url').'/' == $this->curPageURL()) AND $pront == 'false')
                            {
                                return $content;
                            }
                            else
                            {
                               if($up == '2')
        						{
        							return $content.$str;
        						}
        						else
        						{
        							return $str.$content;
        						} 
                            }
					}
					elseif(is_page())
					{
						if( (get_bloginfo('url').'/' == $this->curPageURL()) and $pront == 'false')
                        {
                            return $content;
                        }
                        else
                        {
                           if($up == '2')
    						{
    							return $content.$str;
    						}
    						else
    						{
    							return $str.$content;
    						} 
                        }
					}
					else
					{
							return $content;
					}
				}
			}
		
				
			
        }	
        function meta()
            {
            global $post;
            $option = $this->option;
            $post_id = $post->ID;
            $title = get_post_meta($post_id, 'title', true);
            $type = get_post_meta($post_id, 'type', true);
            $image = get_post_meta($post_id, 'image', true);
            $site_name = get_post_meta($post_id, 'site_name', true);
            $description = get_post_meta($post_id, 'description', true);
            //-----------------------------
            $options = $option['facebook_graph'];
            $title2 = ($title != '')?$title:$options['title'];
            $type2 = ($type != '')?$type:$options['type'];
            $image2 = ($image != '')?$image:$options['image'];
            $site_name2 = ($site_name != '')?$site_name:$options['site_name'];
            $description2 = ($description != '')?$description:$options['description'];
            if(is_single() || is_page())
            {

            if($options['user'] == '1')
            {
       echo '<meta property="og:title" content="'.$title2.'"/>
            <meta property="og:type" content="'.$type2.'"/>
            <meta property="og:url" content="'.get_permalink($post_id).'"/>
            <meta property="og:image" content="'.$image2.'"/>
            <meta property="og:site_name" content="'.$site_name2.'"/>
            <meta property="fb:app_id" content="'.$options['app_id'].'"/>
            <meta property="fb:admins" content="'.$options['admins'].'"/>
            <meta property="og:description" content="'.$description.'"/>';
                  }
                  if($options['contact'] == '1')
                  {
                    echo ' <meta property="og:email" content="'.$options['email'].'"/>
                           <meta property="og:phone_number" content="'.$options['phone_number'].'"/>
                           <meta property="og:fax_number" content="'.$options['fax_number'].'"/>';

                  }
                  if($options['location'] == '1')
                  {
                     echo '<meta property="og:latitude" content="'.$options['latitude'].'"/>
                        <meta property="og:longitude" content="'.$options['longitude'].'"/>
                        <meta property="og:street-address" content="'.$options['street-address'].'"/>
                        <meta property="og:locality" content="'.$options['locality'].'"/>
                        <meta property="og:region" content="'.$options['region'].'"/>
                        <meta property="og:postal-code" content="'.$options['postal-code'].'"/>
                        <meta property="og:country-name" content="'.$options['country-name'].'"/>';
                  }
               }
               else
               {
                if($options['user'] == '1')
                {
                 echo '<meta property="og:title" content="'.$options['title'].'"/>
                <meta property="og:type" content="'.$options['type'].'"/>
                <meta property="og:url" content="'.get_permalink().'"/>
                <meta property="og:image" content="'.$options['image'].'"/>
                <meta property="og:site_name" content="'.$options['site_name'].'"/>
                <meta property="fb:app_id" content="'.$options['app_id'].'"/>
                <meta property="fb:admins" content="'.$options['admins'].'"/>
                <meta property="og:description"
                      content="'.$options['description'].'"/>'."\n";
                }
                      if($options['contact'] == '1')
                  {
                    echo ' <meta property="og:email" content="'.$options['email'].'"/>
                           <meta property="og:phone_number" content="'.$options['phone_number'].'"/>
                           <meta property="og:fax_number" content="'.$options['fax_number'].'"/>'."\n";

                  }
                   if($options['location'] == '1')
                  {
                     echo '<meta property="og:latitude" content="'.$options['latitude'].'"/>
                        <meta property="og:longitude" content="'.$options['longitude'].'"/>
                        <meta property="og:street-address" content="'.$options['street-address'].'"/>
                        <meta property="og:locality" content="'.$options['locality'].'"/>
                        <meta property="og:region" content="'.$options['region'].'"/>
                        <meta property="og:postal-code" content="'.$options['postal-code'].'"/>
                        <meta property="og:country-name" content="'.$options['country-name'].'"/>'."\n";
                  }
               }
            }

 			function admin_menu()  
            {     
                // custom panel for edit post
                add_meta_box( 'Facebook', 'Open Graph Settings <a href="javascript:void(0);" onclick="stFacebookToolboxcustom.user_default_data();">Use default value</a>', array
                (&$this,'draw_panel'), 'post', 'normal', 'high' );
                // custom panel for edit page
                add_meta_box( 'Facebook', 'Open Graph Settings <a href="javascript:void(0);" onclick="stFacebookToolboxcustom.user_default_data();">Use default value</a>', array
                (&$this,'draw_panel'), 'page', 'normal', 'high' );
            }
 			function draw_panel()  
            {
                $controllerName = $this->config['plugin_name'] . "Controller";
		        $controllerObject = new $controllerName($this->config);
		        echo $controllerObject->getAction('draw_panel');
            }

        /*
	 * End edit function
	 */
    public function includeExtraPlugin() {
        // default process here        
    }

    //Initialize hook function

    /**
     * Sample short code
     *
     * Create a short code to show stFacebookToolbox every where
     *
     * @since Plugin expert 1.0
     */
    function showstFacebookToolbox($args) {
        $args = shortcode_atts(array(
                    'stFacebookToolbox_id' => '0'
                        ), $args);

        $controllerName = $this->config['plugin_name'] . "Controller";
        $controllerObject = new $controllerName($this->config);
        return $controllerObject->getAction('showstFacebookToolbox', $args);
    }

    public function custom_field_create() {
        if (function_exists('add_meta_box')) {
            $controllerName = $this->config['plugin_name'] . "Controller";
            $controllerObject = new $controllerName($this->config);
            $controllerObject->actionName = 'custom_field';

            $method = array($controllerObject, 'getAction');
            add_meta_box('custom_field', 'Custom field', $method, 'post', 'normal');
            add_meta_box('custom_field', 'Custom field', $method, 'page', 'normal');
        }
    }

    public function custom_field_handle() {
//        $this->save_attach_file();
//        $this->save_attach_thumb_file();
        if (isset($_POST['post_ID']))
            $pID = $_POST['post_ID'];
        if (isset($_POST['action']) && $_POST['action'] == 'editpost') {
            if (isset($_POST['demo_link'])) {
                $demo_link = $_POST['demo_link'];
                update_post_meta($pID, 'demo_link', $demo_link);
            }
            if (isset($_POST['expert_description'])) {
                $expert_description = $_POST['expert_description'];
                update_post_meta($pID, 'expert_description', $expert_description);
            }
        }
    }
	          public  function curPageURL() {
             $pageURL = 'http';
             if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
             $pageURL .= "://";
             if ($_SERVER["SERVER_PORT"] != "80") {
              $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
             } else {
              $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
             }
             return $pageURL;
            }

}

?>